package org.wmq.javafxmapcollectionstandalone.model.common;

public class MyList<T> {
    public static class Node<T> {
        private T data;
        private Node<T> next;
        private Node<T> prev;

        public Node() {
        }

        public Node(T data, Node<T> next, Node<T> prev) {
            this.data = data;
            this.next = next;
            this.prev = prev;
        }

        /**
         * 获取
         * @return data
         */
        public T getData() {
            return data;
        }

        /**
         * 设置
         * @param data
         */
        public void setData(T data) {
            this.data = data;
        }

        /**
         * 获取
         * @return next
         */
        public Node<T> getNext() {
            return next;
        }

        /**
         * 设置
         * @param next
         */
        public void setNext(Node<T> next) {
            this.next = next;
        }

        /**
         * 获取
         * @return prev
         */
        public Node<T> getPrev() {
            return prev;
        }

        /**
         * 设置
         * @param prev
         */
        public void setPrev(Node<T> prev) {
            this.prev = prev;
        }

        public String toString() {
            return "Node{data = " + data + ", next = " + next + ", prev = " + prev + "}";
        }
    }

    private Node<T> head;
    private Node<T> tail;
    private Node<T> cur;
    private long length;

    public MyList(){}

    public void add(T data){
        Node<T> node = new Node<>(data, null, null);
        if (this.length == 0){
            this.head = node;
            this.tail = node;
            this.cur = node;
            this.length++;
            return;
        }
        this.length++;
        this.tail.setNext(node);
        node.setPrev(this.tail);
        this.tail = node;
    }

    public T getCurrentData(){
        if (this.length == 0){
            return null;
        }
        return this.cur.data;
    }

    public T getFirstData(){
        if (this.length == 0){
            return null;
        }
        return this.head.data;
    }

    public T getLastData() {
        if (this.length == 0){
            return null;
        }
        return this.tail.data;
    }

    public void moveToNext(){
        if (this.length == 0){
            return;
        }
        if (this.cur.next == null){
            this.cur = this.head;
            return;
        }
        this.cur = this.cur.next;
    }

    public void moveToPrev(){
        if (this.length == 0){
            return;
        }
        if (this.cur.prev == null){
            this.cur = this.tail;
            return;
        }
        this.cur = this.cur.prev;
    }

    public void removeCurrentData(){
        if (this.length == 0){
            return;
        }
        if (this.length == 1){
            this.head = null;
            this.tail = null;
            this.cur = null;
            this.length = 0;
            return;
        }
        this.length--;
        if (this.cur == this.head){
            this.head = this.head.next;
            this.head.prev = null;
            this.cur = this.head;
            return;
        }
        if (this.cur == this.tail){
            this.tail = this.tail.prev;
            this.tail.next = null;
            this.cur = this.tail;
            return;
        }
        this.cur.prev.next = this.cur.next;
        this.cur.next.prev = this.cur.prev;
        this.cur = this.cur.prev;
    }

    public void moveToHead(){
        this.cur = this.head;
    }

    public void moveToTail(){
        this.cur = this.tail;
    }
}

